Ext.define('App.crm.salesOfProductsForm', {
    extend: 'Ext.window.Window',
    alias: 'widget.salesOfProductsForm',
    initComponent: function () {
        var form = Ext.widget('form', {
            frame: false,
            border: true,
            split: true,
            autoScroll: true,
            bodyPadding: 5,
            bodyBorder: true,
            fieldDefaults: {
                labelWidth: 75,
                labelAlign: 'right',
                msgTarget: 'side'
            },
            defaults: {
                anchor: '100%'
            },
            items: [
                {xtype: 'hidden',
                    name: 'id',
                    dataIndex: 'id'
                },
                {xtype: 'hidden',
                    name: 'deleteFlag',
                    dataIndex: 'deleteFlag'
                },
                {
                    xtype: 'container',
                    layout: 'hbox',
                    margin: '0 0 5 0',
                    defaults: {
                        flex: 1
                    }, items: [
                    {
                        xtype: 'container',
                        layout: 'hbox',
                        margin: '0 0 5 0',
                        defaults: {

                        }, items: [
                        {xtype: 'hidden',
                            name: 'productId',
                            dataIndex: 'productId',
                            fieldLabel: '产品id'
                        },
                        {xtype: 'textfield',
                            name: 'productName',
                            dataIndex: 'productName',
                            fieldLabel: '产品',
                            readOnly:true,
                            emailText:'请选择产品',
                            width: 230,
                            allowBlank: true
                        },
                        {      xtype: 'button',
                            name: 'btnCustomersId',
                            iconCls: 'searchIcon',
                            scope: this,
                            handler: this.onProductSel,
                            width: 25
                        }
                    ]},
                    {
                        xtype: 'container',
                        layout: 'hbox',
                        margin: '0 0 5 0',
                        defaults: {
                        }, items: [

                        {xtype: 'hidden',
                            name: 'businessOpportunitiesId',
                            dataIndex: 'businessOpportunitiesId'
                        },
                        {xtype: 'textfield',
                            name: 'businessOpportunitiesName',
                            dataIndex: 'businessOpportunitiesName',
                            fieldLabel: '业务名称',
                            readOnly:true,
                            emailText:'请选择业务',
                            width: 230,
                            allowBlank: true
                        },
                        {       xtype: 'button',
                            name: 'btnBusinessOpportunitiesId',
                            iconCls: 'searchIcon',
                            scope: this,
                            handler: this.onBusinessOpportunitiesSel,
                            width: 25
                        }
                    ]}
                ]}
                ,
                {
                    xtype: 'container',
                    layout: 'hbox',
                    margin: '0 0 5 0',
                    defaults: {
                        flex: 1
                    }, items: [

                    new dateTime({
                        name: 'salesDate',
                        dataIndex: 'salesDate',
                        format: 'Y-m-d',
                        fieldLabel: '日期',
                        allowBlank: true
                    }),
                    ,

                    {xtype: 'numberfield',
                        name: 'quantity',
                        dataIndex: 'quantity',
                        allowDecimals: false,
                        decimalPrecision: null,
                        fieldLabel: '数量',
                        allowBlank: true
                    }
                ]}
                ,
                {
                    xtype: 'container',
                    layout: 'hbox',
                    margin: '0 0 5 0',
                    defaults: {
                        flex: 1
                    }, items: [
                    {xtype: 'numberfield',
                        name: 'salesPrice',
                        dataIndex: 'salesPrice',
                        allowDecimals: true,
                        decimalPrecision: 2,
                        fieldLabel: '销售价格',
                        allowBlank: true
                    }
                    ,
                    {xtype: 'numberfield',
                        name: 'totalPrice',
                        dataIndex: 'totalPrice',
                        allowDecimals: true,
                        decimalPrecision: 2,
                        fieldLabel: '总价格',
                        allowBlank: true
                    }
                ]}
                ,

                {xtype: 'textarea',
                    name: 'remark',
                    dataIndex: 'remark',
                    fieldLabel: '产品交易备注',
                    allowBlank: true
                }
            ]
        });
        Ext.apply(this, {
            title: '新增',
            layout: 'fit',
            height: 300,
            width: 550,
            modal: true,
            constrain: true,
            closeAction: 'hide',
            buttonAlign: 'center',
            buttons: [
                {
                    text: "保存",
                    handler: function () {
                        //this.up(‘window’)获取了顶级的window组件。接着使用down()方法获取了window的子组件form组件，最后使用getForm()来获取整个form。
                        var form = this.up('salesOfProductsForm').down('form');
                        if (form.getForm().isValid()) {
                            form.getForm().submit({
                                url: getRootPath() + '/salesOfProductsController/save.action',
                                scope: form,
                                success: function (form, action) {
                                    this.up('salesOfProductsForm').close();
                                    Ext.getCmp('salesOfProductsGrid').getStore().reload();
                                },
                                failure: function (form, action) {
                                    if (action.result.message == undefined)
                                        Ext.Msg.alert('错误', '网络连接失败.');
                                    else
                                        Ext.Msg.alert('错误', action.result.message);
                                },
                                waitMsg: '保存数据...'
                            });
                        } else {
                            Ext.Msg.alert('提示', '表单含有无效或空数据');
                        }
                    }
                },
                {
                    text: "关闭",
                    handler: function () {
                        this.up('salesOfProductsForm').close();
                    }
                }
            ],
            items: form
        });

        this.callParent(arguments);
    },
    businessOpportunitiesWin:null,
    onBusinessOpportunitiesSel: function () {

            if(!this.businessOpportunitiesWin){
                this.businessOpportunitiesWin= new businessOpportunitiesSel();
                this.businessOpportunitiesWin.on('onSelectItem', function(data) {
                	this.down('form').getForm().findField('businessOpportunitiesId').setValue(data.id);
                	this.down('form').getForm().findField('businessOpportunitiesName').setValue(data.bussName);
                }, this);
            }
        this.businessOpportunitiesWin.show();
    } ,productSelWin:null,
    onProductSel:function(){
        if(!this.productSelWin){
            this.productSelWin= new productSel();
            this.productSelWin.on('onSelectItem', function(ids, names) {
            	this.down('form').getForm().findField('productId').setValue(ids);
            	this.down('form').getForm().findField('productName').setValue(names);
                
            }, this);
        }
        this.productSelWin.show();
    }
});